package team.future.joke.util;

import java.nio.charset.Charset;

/**
 * 简单加密解密
 * Title: SecurityUtil.java<br>
 * Description: <br>
 * Copyright (c) 未来工作室版权所有 2015    <br>
 * Create DateTime: 2015年9月2日 下午12:26:06 <br>
 * @author futurezhu
*/
public class SecurityUtil {
	/**
	 * 加密核心字符串
	 */
	private static final String key = "<%future&team%>";
	/**
	 * 编码
	 */
	private static final Charset charset = Charset.forName("UTF-8");
	private static byte[] keyBytes = key.getBytes(charset);

	/**
	 * 加密
	 * @param enc
	 * @return
	 */
	public static String encode(String enc) {
		byte[] b = enc.getBytes(charset);
		for (int i = 0, size = b.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				b[i] = (byte) (b[i] ^ keyBytes0);
			}
		}
		return new String(b);
	}

	/**
	 * 解密
	 * @param dec
	 * @return
	 */
	public static String decode(String dec) {
		byte[] e = dec.getBytes(charset);
		byte[] dee = e;
		for (int i = 0, size = e.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				e[i] = (byte) (dee[i] ^ keyBytes0);
			}
		}
		return new String(e);
	}

	public static void main(String[] args) {
		String s = "future";
		String enc = encode(s);
		String dec = decode(enc);
		System.out.println(enc);
		System.out.println(dec);
	}
}
